<template>
	<!-- 总容器 -->
	<div class="wrapper">
	  <!-- header部分 -->
	  <header>
		<p>更新密码</p>
	  </header>
  
	  <!-- 更新密码部分 -->
	  <div class="form-box">
		<li>
		  <div class="title">旧密码:</div>
		  <div class="content">
			<input type="password" v-model="oldPass" placeholder="旧密码" />
		  </div>
		</li>
		<li>
		  <div class="title">新密码:</div>
		  <div class="content">
			<input type="password" v-model="newPass" placeholder="新密码" />
		  </div>
		</li>
	  </div>
  
	  <!-- 保存按钮 -->
	  <div class="save-button" @click="toUpdateUserPassword">
		<button>保存</button>
	  </div>
  
	  <!-- 底部菜单部分 -->
	  <Footer />
	</div>
  </template>
  
  <script>
  import { ref, onMounted } from 'vue'; // 引入Vue 3的ref、生命周期函数
  import axios from 'axios'; // 引入axios
  import Footer from '../components/Footer.vue'; // 引入Footer组件
  import { getSessionStorage, setSessionStorage } from '../common.js'; // 引入自定义的storage操作方法
  
  export default {
	name: 'UpdateUserPassword',
	components: {
	  Footer,
	},
	setup() {
	  const oldPass = ref(''); // 旧密码
	  const newPass = ref(''); // 新密码
	  const user = ref({}); // 用户信息
  
	  // 在组件挂载时获取用户信息
	  onMounted(() => {
		const storedUser = getSessionStorage('user');
		if (storedUser) {
		  user.value = storedUser;
		}
	  });
  
	  // 更新密码的逻辑
	  const toUpdateUserPassword = () => {
		if (oldPass.value.trim() === '') {
		  alert('请输入旧密码！');
		  return;
		}
		if (newPass.value.trim() === '') {
		  alert('新密码不能为空！');
		  return;
		}
  
		// 发送 PUT 请求更新密码
		axios
		  .put('/User/userId/oldPass/newPass', new URLSearchParams({
			userId: user.value.userId,
			oldPass: oldPass.value,
			newPass: newPass.value,
		  }))
		  .then(response => {
			if (response.data > 0) {
			  // 更新用户信息并保存到 sessionStorage
			  user.value.password = newPass.value;
			  setSessionStorage('user', user.value);
			  alert('修改密码成功！');
			} else {
			  alert('修改密码失败！');
			}
		  })
		  .catch(error => {
			console.error('Error updating password:', error);
		  });
	  };
  
	  return {
		oldPass,
		newPass,
		toUpdateUserPassword,
	  };
	},
  };
  </script>
  
  <style scoped>
  /****************** 总容器 ******************/
  .wrapper {
	width: 100%;
	height: 100%;
  }
  
  /****************** header ******************/
  .wrapper header {
	width: 100%;
	height: 12vw;
	background-color: #0097ff;
	color: #fff;
	font-size: 4.8vw;
	position: fixed;
	left: 0;
	top: 0;
	z-index: 1000;
	display: flex;
	justify-content: center;
	align-items: center;
  }
  
  /****************** 修改密码部分 ******************/
  .wrapper .form-box {
	width: 100%;
	padding-top: 12vw;
  }
  
  .wrapper .form-box li {
	display: flex;
	align-items: center;
	box-sizing: border-box;
	padding: 3vw 3vw 0;
  }
  
  .wrapper .form-box li .title {
	flex: 0 0 18vw;
	font-size: 3.5vw;
	font-weight: 700;
	color: darkslategray;
  }
  
  .wrapper .form-box li .content {
	flex: 1;
  }
  
  .wrapper .form-box li .content input {
	width: 100%;
	box-sizing: border-box;
	padding: 4vw 2vw;
	border-radius: 3vw;
	background-color: white;
	font-size: 3vw;
	border: none;
	outline: none;
  }
  
  /****************** 保存按钮部分 ******************/
  .wrapper .save-button {
	width: 100%;
	box-sizing: border-box;
	padding: 4vw 10vw 0;
  }
  
  .wrapper .save-button button {
	width: 100%;
	height: 10vw;
	background-color: mediumseagreen;
	border: none;
	outline: none;
	border-radius: 4vw;
	font-size: 3.5vw;
	color: white;
	cursor: pointer;
  }
  </style>
  